<?php

namespace app\web\filter;

use app\common\component\Param;
use app\common\component\ErrorCode;

/**
 * 登录控制器过滤类
 * Class IndexFilter
 * @package app\web\filter
 */
class LoginFilter
{
    /**
     * xss过滤
     * @param $string
     * @return mixed|null|string|string[]
     */
    private function cleanXss($string)
    {
        $string = trim($string);
        $string = strip_tags($string);
//        $string = htmlspecialchars ( $string );
//        $string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "//" ), '', $string );
        $no = '/%0[0-8bcef]/';
        $string = preg_replace($no, '', $string);
        $no = '/%1[0-9a-f]/';
        $string = preg_replace ($no, '', $string);
        $no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';
        $string = preg_replace ( $no, '', $string );
        return $string;
    }

    /**
     * 发送短信验证码
     * @throws \think\Exception
     */
    public function emailPost()
    {
        if (!isset(Param::$inputParams['email'], Param::$inputParams['scene_type'])) {
            throw new \think\Exception('参数格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (filter_var(Param::$inputParams['email'], FILTER_VALIDATE_EMAIL) === false) {
            throw new \think\Exception('邮箱格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (!in_array(Param::$inputParams['scene_type'], [1, 2])) {
            throw new \think\Exception('场景值错误', ErrorCode::RESPONSE_FAIL);
        }

        Param::$validParams['email'] = Param::$inputParams['email'];
        Param::$validParams['scene_type'] = Param::$inputParams['scene_type'];
    }

    /**
     * 发送短信验证码
     * @throws \think\Exception
     */
    public function registerPost()
    {
        if (!isset(Param::$inputParams['email'], Param::$inputParams['email_code'], Param::$inputParams['password'], Param::$inputParams['confirm_password'], Param::$inputParams['nickname'])) {
            throw new \think\Exception('参数格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (filter_var(Param::$inputParams['email'], FILTER_VALIDATE_EMAIL) === false) {
            throw new \think\Exception('邮箱格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_string(Param::$inputParams['email_code']) || strlen(Param::$inputParams['email_code']) != 6) {
            throw new \think\Exception('验证码格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_string(Param::$inputParams['password']) || strlen(Param::$inputParams['password']) < 6 || strlen(Param::$inputParams['password']) > 15) {
            throw new \think\Exception('密码应为6-15位', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_string(Param::$inputParams['confirm_password']) || Param::$inputParams['confirm_password'] != Param::$inputParams['password']) {
            throw new \think\Exception('确认密码错误', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_string(Param::$inputParams['nickname'])) {
            throw new \think\Exception('昵称格式错误', ErrorCode::RESPONSE_FAIL);
        }

        $nickname = $this->cleanXss(Param::$inputParams['nickname']);
        if ($nickname === '' || mb_strlen(Param::$inputParams['nickname'], 'utf-8') > 10) {
            throw new \think\Exception('昵称不能为空或者超过10字符', ErrorCode::RESPONSE_FAIL);
        }

        Param::$validParams['email'] = Param::$inputParams['email'];
        Param::$validParams['email_code'] = Param::$inputParams['email_code'];
        Param::$validParams['password'] = Param::$inputParams['password'];
        Param::$validParams['confirm_password'] = Param::$inputParams['confirm_password'];
        Param::$validParams['nickname'] = $this->cleanXss(Param::$inputParams['nickname']);
    }

    /** 登录校验 */
    public function loginPost()
    {
        if (!isset(Param::$inputParams['email'], Param::$inputParams['password'])) {
            throw new \think\Exception('参数格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (filter_var(Param::$inputParams['email'], FILTER_VALIDATE_EMAIL) === false) {
            throw new \think\Exception('邮箱格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_string(Param::$inputParams['password']) || strlen(Param::$inputParams['password']) < 6 || strlen(Param::$inputParams['password']) > 15) {
            throw new \think\Exception('密码应为6-15位', ErrorCode::RESPONSE_FAIL);
        }

        Param::$validParams['email'] = Param::$inputParams['email'];
        Param::$validParams['password'] = Param::$inputParams['password'];
    }

    public function forgetPost()
    {
        if (!isset(Param::$inputParams['email'], Param::$inputParams['email_code'], Param::$inputParams['password'], Param::$inputParams['confirm_password'])) {
            throw new \think\Exception('参数格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (filter_var(Param::$inputParams['email'], FILTER_VALIDATE_EMAIL) === false) {
            throw new \think\Exception('邮箱格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_string(Param::$inputParams['email_code']) || strlen(Param::$inputParams['email_code']) != 6) {
            throw new \think\Exception('验证码格式错误', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_string(Param::$inputParams['password']) || strlen(Param::$inputParams['password']) < 6 || strlen(Param::$inputParams['password']) > 15) {
            throw new \think\Exception('密码应为6-15位', ErrorCode::RESPONSE_FAIL);
        }

        if (!is_string(Param::$inputParams['confirm_password']) || Param::$inputParams['confirm_password'] != Param::$inputParams['password']) {
            throw new \think\Exception('确认密码错误', ErrorCode::RESPONSE_FAIL);
        }

        Param::$validParams['email'] = Param::$inputParams['email'];
        Param::$validParams['email_code'] = Param::$inputParams['email_code'];
        Param::$validParams['password'] = Param::$inputParams['password'];
        Param::$validParams['confirm_password'] = Param::$inputParams['confirm_password'];
    }
}